/**
 * 
 */
package com.kx.center.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;


/**
 * Date: 2014年12月12日
 * @Author: Eric.Li	
 */
public class Localization {
	
    private static java.util.Properties properties = new java.util.Properties();
    
    static{
    	try {
    		String filepath = Localization.class.getClassLoader().getResource("language.zh_cn.properties").getPath();
    		File file = new File(filepath);
    		properties.load(new FileInputStream(file));
    	} catch (IOException e) {
    		e.printStackTrace();
    		throw new RuntimeException("Couldn't load application.properties");
        }
    }
	
	public static Object get(String key){
		key = key.toUpperCase();
		if(properties.containsKey(key)){
			return properties.get(key);
		}
		return null;
	}
	
	public static String getString(String key, Object... objects){
		Object object = get(key);
		if(object != null){
			String str = object.toString().trim();
			
			if(StringUtils.isBlank(str))
				return str;
			
			if(objects != null && objects.length > 0) {
				Pattern pattern = Pattern.compile("\\{\\s*([0-9_]+)\\s*\\}");
				Matcher matcher = pattern.matcher(str);
				
				while(matcher.find()) {
					String oldStr = matcher.group(0);
					int val = Integer.valueOf(matcher.group(1));
					str = str.replace(oldStr, objects[val].toString());
				}
			}
			
			return str;
		}
		return null;
	}

}
